﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Helper.Infrastructure.Extensions
{
    public static class Utils
    {
        public static string IntoString(this string[] array, string delimiter)
        {
            if (array == null)
                return null;

            string result = "";

            try
            {
                if (array.Length > 0)
                {
                    for (int i = 0; i < array.Length; i++)
                        result += string.Format("{0}{1}", delimiter, array[i]);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return result;
        }

        public static string IntoString(this string[] array, char delimiter)
        {
            return array.IntoString(delimiter.ToString());
        }

        public static string IntoString(this string[] array)
        {
            return array.IntoString(",");
        }

        public static string[] IntoStringArray(this string str, string delimiter)
        {
            if (str == null)
                return null;

            try
            {
                return str.Split(delimiter.ToCharArray());
            }
            catch (Exception ex)
            {                
                throw new Exception(ex.Message);
            }
        }

        public static string[] IntoStringArray(this string str)
        {
            return str.IntoStringArray(",");
        }

        public static DateTime RemoveAllTime(this DateTime date)
        {
            date = date.AddHours(-date.Hour);
            date = date.AddMinutes(-date.Minute);
            date = date.AddSeconds(-date.Second);
            date = date.AddMilliseconds(-date.Millisecond);
            return date;
        }

        public static DateTime SetTimeTop(this DateTime date)
        {
            date = date.AddHours((23-date.Hour));
            date = date.AddMinutes((59-date.Minute));
            date = date.AddSeconds((59-date.Second));
            date = date.AddMilliseconds((999-date.Millisecond));
            return date;
        }
    }
}
